package hos.util.utils;

import android.content.ContentValues;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import hos.java.util.MapUtils;

/**
 * <p>Title: MapUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2021/3/22 14:39
 */
public class ContentValueUtils {
    /**
     * 将map转换为List
     *
     * @return List
     */

    public static List<String> toListKey(ContentValues map) {
        if (map == null || map.size() == 0) {
            return new LinkedList<>();
        }
        final Set<String> keySet = map.keySet();
        return new LinkedList<>(keySet);
    }

    /**
     * 将map转换为List
     *
     * @return List
     */

    public static List<Object> toListValue(ContentValues map) {
        if (map == null || map.size() == 0) {
            return new ArrayList<>();
        }
        final List<Object> list = new ArrayList<>();
        final Set<String> keySet = map.keySet();
        final int size = keySet.size();
        int i = 0;
        for (String key : keySet) {
            Object item = map.get(key);
            if (item == null) {
                continue;
            }
            list.add(item);
            i++;
        }
        return list;
    }

    public static Object or(ContentValues target, String key, Object defaultValue) {
        if (target == null || target.size() == 0) {
            return defaultValue;
        }
        Object v = target.get(key);
        if (v == null) {
            return defaultValue;
        } else if (("null".equals(v) || "NULL".equals(v))) {
            return defaultValue;
        } else
            return v;
    }

    public static Object orNull(ContentValues target, String key) {
        return or(target, key, null);
    }

    public static Object orEmpty(ContentValues target, String key) {
        return or(target, key, "");
    }

    /**
     * 将ContentValues转换成Map
     *
     * @param values
     * @return
     */
    public static Map<String, Object> convert(ContentValues values) {
        Map<String, Object> map = new HashMap<>();
        Set<String> keySet = values.keySet();
        for (String key : keySet) {
            map.put(key, orEmpty(values, key));
        }
        return map;
    }

    /**
     * 将ContentValues转换成Map
     *
     * @param values
     * @return
     */
    public static Map<String, String> convertString(ContentValues values) {
        Map<String, String> map = new HashMap<>();
        Set<String> keySet = values.keySet();
        for (String key : keySet) {
            map.put(key, orEmpty(values, key).toString());
        }
        return map;
    }

    public static ContentValues convert(Map<String, Object> values) {
        ContentValues contentValues = new ContentValues();
        Set<String> keySet = values.keySet();
        for (String key : keySet) {
            contentValues.put(key, MapUtils.empty(values, key));
        }
        return contentValues;
    }

    public static ContentValues convertString(Map<String, String> values) {
        ContentValues contentValues = new ContentValues();
        Set<String> keySet = values.keySet();
        for (String key : keySet) {
            contentValues.put(key, MapUtils.empty(values, key));
        }
        return contentValues;
    }
}
